package service

import (
	"fmt"

	"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

const Ak string = "JKVW6GZO1YLJLRNNWWPR"
const Sk string = "gny0QEZMiKjeHuC6UDw7wCBXiNC7NgCojkxk97Hs"
const Endpoint string = "obs.ap-southeast-1.myhuaweicloud.com"
const backName string = "arab-php"
const baseUrl string = "http://file.arab-alabo.com/"

// const downUrl string = "https://arab-php.obs.ap-southeast-1.myhuaweicloud.com:443/"

func Obsupload(tmp_file string, file_name string) {

	obsClient, err := obs.New(Ak, Sk, Endpoint)
	if err != nil {
		fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
	}
	input := &obs.PutFileInput{}
	input.Bucket = backName
	input.Key = file_name
	input.SourceFile = tmp_file

	output, err := obsClient.PutFile(input)
	if err == nil {
		fmt.Println("++++++++++++++" + baseUrl + file_name + "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
		fmt.Printf("StorageClass:%s, ETag:%s\n", output.StorageClass, output.ETag)
		return
	}
	fmt.Printf("Put file(%s) under the bucket(%s) fail!\n", input.Key, input.Bucket)
	if obsError, ok := err.(obs.ObsError); ok {
		fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.")
		fmt.Println(obsError.Error())
	} else {
		fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.")
		fmt.Println(err)
	}
}
